package com.jun.common.core.exception;


import com.jun.common.core.entity.page.ResultHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.Objects;

/**
 * @author yuejun.li
 * @date 2023/7/10 09:55:49
 * 全局捕获异常
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultHelper handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return ResultHelper.fail("请求地址" + requestURI + "," + "不支持" + e.getMethod() + "请求");
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResultHelper handleServiceException(ServiceException e, HttpServletRequest request) {
        log.error(e.getMessage(), e);
        long code = Long.valueOf(e.getCode());
        String message = Objects.nonNull(e.getMessage()) ? e.getMessage() : "操作失败";
        return Objects.nonNull(code) ? ResultHelper.fail(message) : ResultHelper.fail(e.getMessage());
    }

    /**
     * 参数校验
     * java bean参数校验
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseStatus(HttpStatus.OK)
    public ResultHelper handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder sb = new StringBuilder("校验失败:");
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            sb.append(fieldError.getField()).append("：").append(fieldError.getDefaultMessage()).append(", ");
        }
        String msg = sb.toString();
        return ResultHelper.fail(msg);
    }

    /**
     * 参数校验
     * 非java bean参数校验
     */
    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseStatus(HttpStatus.OK)
    public ResultHelper handleConstraintViolationException(ConstraintViolationException ex) {
        return ResultHelper.fail(ex.getMessage());
    }

    /**
     * 参数校验
     * 非java bean参数校验
     */
    /*@ExceptionHandler({UsernameNotFoundException.class})
    @ResponseStatus(HttpStatus.OK)
    public ResultHelper handleUsernameNotFoundException(UsernameNotFoundException ex) {
        return ResultHelper.fail(ex.getMessage());
    }*/
}
